//Nordic-Sauna JavaScript Functions

function check_form() {
	errors = false;
	errorMsg = "";
	errorCount = 0;
	form = document.checkout;
	
	// basic check required fields
	if (form.firstName.value == "" || form.firstName.value.length < 3)
	{
		errorMsg += "Please provide a valid first name.\n";
		errors = true;
		if (errorCount == 0) form.firstName.focus();
		errorCount++;		
  	}
	if (form.lastName.value == "" || form.firstName.value.length < 3)
	{
		errorMsg += "Please provide a valid last name.\n";
		errors = true;
		if (errorCount == 0) form.lastName.focus();
		errorCount++;
	}
	if (form.email.value == "")
	{
		errorMsg += "Please provide an e-mail address.\n";
		errors = true;
		if (errorCount == 0) form.email.focus();
		errorCount++;
	}
	else if (form.email.value != "")
	{
		/* From: http://www.w3schools.com/js/js_form_validation.asp
		The function below checks if the content has the general syntax of an email.
		This means that the input data must contain at least an @ sign and a dot (.).
		Also, the @ must not be the first character of the email address, and the last dot must at least be one character after the @ sign:
		*/
		
		atPosition = form.email.value.indexOf("@");
		dotPosition = form.email.value.lastIndexOf(".");
		
		if (atPosition < 1 || dotPosition - atPosition < 2 || form.email.value.length < 6)
		{
			errorMsg += "Please provide a valid e-mail address.\n";
			errors = true;
			if (errorCount == 0) form.email.focus();
			errorCount++;
		}
	}
	if (form.billStreet.value == "" || form.billStreet.length < 8)
	{
		errorMsg += "Please provide a valid billing street address.\n";
		errors = true;
		if (errorCount == 0) form.billStreet.focus();
		errorCount++;
	}
	if (form.billSuburb.value == "" || form.billSuburb.length < 8)
	{
		errorMsg += "Please provide a valid billing suburb.\n";
		errors = true;
		if (errorCount == 0) form.billSuburb.focus();
		errorCount++;
	}
	if (form.billCity.value == "" || form.billCity.length < 8)
	{
		errorMsg += "Please provide a valid billing city.\n";
		errors = true;
		if (errorCount == 0) form.billCity.focus();
		errorCount++;
	}
	if (form.billState.value == "" || form.billState.length < 8)
	{
		errorMsg += "Please provide a valid billing state.\n";
		errors = true;
		if (errorCount == 0) form.billState.focus();
		errorCount++;
	}
	if (form.billPostcode.value == "" || form.billPostcode.length < 3 || form.billPostcode.length != 4 && form.billCountry == "Australia")
	{
		errorMsg += "Please provide a valid billing postcode.\n";
		errors = true;
		if (errorCount == 0) form.billPostcode.focus();
		errorCount++;
	}
	
	//Check only if shipping to another address
	if (form.shipToBill[1].checked)
	{
		if (form.shipStreet.value == "" || form.shipStreet.length < 8)
		{
			errorMsg += "Please provide a valid shipping street address.\n";
			errors = true;
			if (errorCount == 0) form.shipStreet.focus();
			errorCount++;
		}
		if (form.shipSuburb.value == "" || form.shipSuburb.length < 8)
		{
			errorMsg += "Please provide a valid shipping suburb.\n";
			errors = true;
			if (errorCount == 0) form.shipSuburb.focus();
			errorCount++;
		}
		if (form.shipCity.value == "" || form.shipCity.length < 8)
		{
			errorMsg += "Please provide a valid shipping city.\n";
			errors = true;
			if (errorCount == 0) form.shipCity.focus();
			errorCount++;
		}
		if (form.shipState.value == "" || form.shipState.length < 8)
		{
			errorMsg += "Please provide a valid shipping state.\n";
			errors = true;
			if (errorCount == 0) form.shipState.focus();
			errorCount++;
		}
		if (form.shipPostcode.value == "" || form.shipPostcode.length < 3 || form.shipPostcode.length != 4 && form.shipCountry == "Australia")
		{
			errorMsg += "Please provide a valid shipping postcode.\n";
			errors = true;
			if (errorCount == 0) form.shipPostcode.focus();
			errorCount++;
		}
	}
	if (form.paymentMethod[0].checked || form.paymentMethod[1].checked)
	{
		if (form.cardholderName.value == "")
		{
			errorMsg += "Please provide a cardholder name.\n";
			errors = true;
			if (errorCount == 0) form.cardholderName.focus();
			errorCount++;
		}
		if (form.cardNumber.value == "" || form.cardNumber.value.length < 16)
		{
			errorMsg += "Please provide a valid card number.\n";
			errors = true;
			if (errorCount == 0) form.cardNumber.focus();
			errorCount++;
		}
		if (form.cvc.value == "" || form.cvc.value.length < 3)
		{
			errorMsg += "Please provide a valid CVC/verification number.\n";
			errors = true;
			if (errorCount == 0) form.cvc.focus();
			errorCount++;		
  		}
  	}
	//form.paymentMethod[0].checked
	// message if errors exist
	if (errors == true)
	{
		alert("You have " + errorCount + " errors.\n" + errorMsg);
		return !errors;
	}	
}

function check_form2() {
	errors = false;
	errorMsg = "";
	errorCount = 0;
	form = document.signup;
	
	// basic check required fields
	if (form.firstName.value == "" || form.firstName.value.length < 2)
	{
		errorMsg += "Please provide a valid first name.\n";
		errors = true;
		if (errorCount == 0) form.firstName.focus();
		errorCount++;		
  	}
	if (form.lastName.value == "" || form.firstName.value.length < 2)
	{
		errorMsg += "Please provide a valid last name.\n";
		errors = true;
		if (errorCount == 0) form.lastName.focus();
		errorCount++;
	}
	if (form.email.value == "")
	{
		errorMsg += "Please provide an e-mail address.\n";
		errors = true;
		if (errorCount == 0) form.email.focus();
		errorCount++;
	}
	else if (form.email.value != "")
	{
		/* From: http://www.w3schools.com/js/js_form_validation.asp
		The function below checks if the content has the general syntax of an email.
		This means that the input data must contain at least an @ sign and a dot (.).
		Also, the @ must not be the first character of the email address, and the last dot must at least be one character after the @ sign:
		*/
		
		atPosition = form.email.value.indexOf("@");
		dotPosition = form.email.value.lastIndexOf(".");
		
		if (atPosition < 1 || dotPosition - atPosition < 2 || form.email.value.length < 6)
		{
			errorMsg += "Please provide a valid e-mail address.\n";
			errors = true;
			if (errorCount == 0) form.email.focus();
			errorCount++;
		}
	}
	if (form.street.value == "" || form.street.length < 8)
	{
		errorMsg += "Please provide a valid street address.\n";
		errors = true;
		if (errorCount == 0) form.street.focus();
		errorCount++;
	}
	if (form.suburb.value == "" || form.suburb.length < 8)
	{
		errorMsg += "Please provide a valid suburb.\n";
		errors = true;
		if (errorCount == 0) form.suburb.focus();
		errorCount++;
	}
	if (form.city.value == "" || form.city.length < 8)
	{
		errorMsg += "Please provide a valid city.\n";
		errors = true;
		if (errorCount == 0) form.city.focus();
		errorCount++;
	}
	if (form.state.value == "" || form.state.length < 8)
	{
		errorMsg += "Please provide a valid state.\n";
		errors = true;
		if (errorCount == 0) form.state.focus();
		errorCount++;
	}
	if (form.postcode.value == "" || form.postcode.length < 3 || form.postcode.length != 4 && form.country == "Australia")
	{
		errorMsg += "Please provide a valid postcode.\n";
		errors = true;
		if (errorCount == 0) form.postcode.focus();
		errorCount++;
	}
	if (form.username.value == "" || form.username.value.length < 6)
	{
		errorMsg += "Please provide a valid username that has at least 6 characters.\n";
		errors = true;
		if (errorCount == 0) form.username.focus();
		errorCount++;
	}
	if (form.password.value == "" || form.confirmPassword.value == "")
	{
		errorMsg += "Please provide a password.\n";
		errors = true;
		if (errorCount == 0) form.password.focus();
		errorCount++;
	}
	if (form.password.value != form.confirmPassword.value)
	{
		errorMsg += "Please confirm your password correctly.\n";
		errors = true;
		if (errorCount == 0) form.password.focus();
		errorCount++;
	}
	//form.paymentMethod[0].checked
	// message if errors exist
	if (errors == true)
	{
		alert("You have " + errorCount + " errors.\n" + errorMsg);
		return !errors;
	}	
}

function copy_value()
{
	form = document.checkout;
	form.shipCompanyName.value=form.billCompanyName.value;
	form.shipStreet.value=form.billStreet.value;
	form.shipSuburb.value=form.billSuburb.value;
	form.shipCity.value=form.billCity.value;
	form.shipState.value=form.billState.value;
	form.shipPostcode.value=form.billPostcode.value;
	form.shipCountry.value=form.billCountry.value;
}

function ship_to_billing_address(tick)
{
	form = document.checkout;
	if (tick == false)
	{
		form.shipCompanyName.disabled=false;
		form.shipStreet.disabled=false;
		form.shipSuburb.disabled=false;
		form.shipCity.disabled=false;
		form.shipState.disabled=false;
		form.shipPostcode.disabled=false;
		form.shipCountry.disabled=false;
	}
	else
	{
		form.shipCompanyName.value=form.billCompanyName.value;
		form.shipStreet.value=form.billStreet.value;
		form.shipSuburb.value=form.billSuburb.value;
		form.shipCity.value=form.billCity.value;
		form.shipState.value=form.billState.value;
		form.shipPostcode.value=form.billPostcode.value;
		form.shipCountry.value=form.billCountry.value;
		
		form.shipCompanyName.disabled=false;
		form.shipStreet.disabled=false;
		form.shipSuburb.disabled=false;
		form.shipCity.disabled=false;
		form.shipState.disabled=false;
		form.shipPostcode.disabled=false;
		form.shipCountry.disabled=false;
	}
}